﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.WebControls;
using System.Web.UI;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;

namespace CaptchaFieldControlSolution
{
    /// <summary>
    /// This class is the editor for the CaptchaFieldControl
    /// </summary>
    public class CaptchaFieldControlEditor : UserControl, IFieldEditor
    {
        private SPField Field { get; set; }

        protected TextBox tbRecaptchaPublicKey;

        protected TextBox tbRecaptchaPrivateKey;

        protected TextBox tbRecaptchaTheme;

        #region IFieldEditor Members
        public bool DisplayAsNewSection
        {
            get
            {
                return true;
            }
        }

        public void InitializeWithField(SPField field)
        {
            Field = field;
        }

        protected override void CreateChildControls()
        {
            if (Field != null)
            {
                // An existing field/column is being edited.
                // Get the field and the values.
                CaptchaField f = (CaptchaField)Field;
                tbRecaptchaPublicKey.Text = f.RecaptchaPublicKey;
                tbRecaptchaPrivateKey.Text = f.RecaptchaPrivateKey;
                tbRecaptchaTheme.Text = f.RecaptchaTheme;
            }

            base.CreateChildControls();
        }

        /// <summary>
        /// Set properties on field
        /// </summary>
        /// <param name="field"></param>
        /// <param name="isNewField"></param>
        public void OnSaveChange(SPField field, bool isNewField)
        {
            CaptchaField f = (CaptchaField)field;
            f.RecaptchaPublicKey = tbRecaptchaPublicKey.Text;
            f.RecaptchaPrivateKey = tbRecaptchaPrivateKey.Text;
            f.RecaptchaTheme = tbRecaptchaTheme.Text;
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
        }

        #endregion
    }
}
